package com.xiongxiong.yiqing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiongxiong.yiqing.exception.Asserts;
import com.xiongxiong.yiqing.mapper.SysUserMapper;
import com.xiongxiong.yiqing.pojo.SysUser;
import com.xiongxiong.yiqing.pojo.vo.FindSysUserCondition;
import com.xiongxiong.yiqing.pojo.vo.LoginVO;
import com.xiongxiong.yiqing.pojo.vo.SysUserVO;
import com.xiongxiong.yiqing.service.SysUserService;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 熊熊很酷
 * @since 2022-12-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public LoginVO login(SysUserVO sysUserVO) {
        SysUser userInfo = getUserInfo(sysUserVO);
        if(userInfo == null){
            Asserts.fail("账号还未注册");
        }else {
            if (userInfo.getPassword().equals(sysUserVO.getPassword())) {
                LoginVO loginVO = new LoginVO();
                loginVO.setId(userInfo.getId());
                loginVO.setUsername(userInfo.getPhoneNub());
                loginVO.setRoles(Arrays.asList(userInfo.getRole()));
                return loginVO;
            }
            Asserts.fail("账号密码错误");
        }
        return null;
    }

    /**
     * 用户注册
     * @param sysUserVO vo
     * @return com.xiongxiong.yiqing.pojo.vo.SysUserVO
     * @author 熊林飞
     * @date: 2022/12/14 15:11
     */
    @Override
    public SysUserVO register(SysUserVO sysUserVO) {
        SysUser sysUser = getUserInfo(sysUserVO);
        if (sysUser == null) {
            sysUser = new SysUser();
            BeanUtil.copyProperties(sysUserVO, sysUser, String.valueOf(true));
            sysUser.setAddress("萍乡学院");
            save(sysUser);
        } else {
            Asserts.fail("用户已存在");
        }
        sysUserVO.setPassword(null);
        return sysUserVO;
    }

    /**
     * 查询用户信息
     * @param sysUserVO vo
     * @return com.xiongxiong.yiqing.pojo.SysUser
     * @author 熊林飞
     * @date: 2022/12/14 14:45
     */
    private SysUser getUserInfo(SysUserVO sysUserVO) {
        SysUser sysUser = this.getOne(new QueryWrapper<SysUser>().eq("phone_nub", sysUserVO.getPhoneNub()));
        return sysUser;
    }

    /**
     * 分页或帅选条件查询
     * @param pageNub 第几页
     * @param pageSize 每页多大
     * @param findCondition 筛选条件 可不传
     * @author 熊林飞
     * @date: 2023/2/11 13:41
     */
    @Override
    public Page<SysUser> selectUserListByPage(Integer pageNub, Integer pageSize, FindSysUserCondition findCondition) {
        Page<SysUser> page = new Page<>(pageNub, pageSize);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.select("id","phone_nub","user_name","sex","address","role","create_time");

        System.out.println(findCondition);

        // 进行条件筛选
        if (findCondition != null){
            if(StrUtil.isNotBlank(findCondition.getUserName())){
                wrapper.like("user_name", findCondition.getUserName());
            }
            if(StrUtil.isNotBlank(findCondition.getPhoneNub())){
                wrapper.like("phone_nub", findCondition.getPhoneNub());
            }
            if(StrUtil.isNotBlank(findCondition.getAddress())){
                wrapper.like("address", findCondition.getAddress());
            }
        }

        Page<SysUser> userPage = this.page(page, wrapper);
        return userPage;
    }

    /**
     * 更新用户
     * @param user
     * @return java.lang.Boolean
     * @author 熊林飞
     * @date: 2023/2/11 20:45
     */
    @Override
    public Boolean updateUserById(SysUser user) {
        if(user == null || user.getId() == null){
            Asserts.fail("操作异常");
        }
        SysUser sysUser = this.getOne(new QueryWrapper<SysUser>().eq("phone_nub", user.getPhoneNub()));
        if (sysUser != null) {
            if (sysUser.getId().equals(user.getId())){

            }else {
                Asserts.fail("该电话号码已经被注册");
            }
        }
        return this.updateById(user);
    }

    /**
     * 删除通过id
     * @param id 用户id
     * @author 熊林飞
     * @date: 2023/2/11 21:03
     */
    @Override
    public Boolean deleteUserById(Integer id) {
        if (id == 1001){
            Asserts.fail("该管理员账号不可删除");
        }
        return this.removeById(id);
    }

    /**
     * 查询通过id
     * @param id 用户id
     * @return com.xiongxiong.yiqing.pojo.SysUser
     * @author 熊林飞
     * @date: 2023/2/11 22:27
     */
    @Override
    public SysUser selectUserById(Integer id) {
        SysUser user = this.getById(id);
        if(user == null){
            return null;
        }
        user.setPassword(null);
        return user;
    }

    /**
     * 修改密码
     * @param id 用户id
     * @param newPassword 新密码
     * @author 熊林飞
     * @date: 2023/2/11 22:35
     */
    @Override
    public Boolean updatePassword(Integer id, String newPassword) {
        if (id == null || newPassword == null){
            Asserts.fail("老实点");
        }
        boolean update = this.update(new UpdateWrapper<SysUser>().set("password", newPassword).eq("id", id));
        return update;
    }
}
